package com.centerm.demo;

import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.centerm.bluetooth.jf.CCTSwiperController;
import com.centerm.bluetooth.jf.constants.Template;
import com.centerm.bluetooth.jf.listener.CSwiperStateChangedListener;
import com.centerm.demo.jf.R;
import com.centerm.util.Logger;


public class MainActivity extends AppCompatActivity {

    private CCTSwiperController cctSwiperController;
    private CSwiperStateChangedImp cSwiperStateChangedImp;
    private TextView txtvw;
    private BluetoothDialog bluetoothDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtvw = (TextView) findViewById(R.id.txtvw);
        cSwiperStateChangedImp = new CSwiperStateChangedImp();
        cctSwiperController = new CCTSwiperController(getApplicationContext(), cSwiperStateChangedImp);
        Logger.setDEBUG(true,true);
        bluetoothDialog = new BluetoothDialog(this);
    }

    public void onSeach(View v) {
        bluetoothDialog.show();
        cctSwiperController.onStartSearchBluetooth();
    }

    public void onConnect(View v) {
        cctSwiperController.onStopSearchBluetooth();
        BluetoothDevice device = bluetoothDialog.getSel();
        if (device != null)
            cctSwiperController.onConnectBluetooth(60000, device.getAddress());
    }

    public void onVersionClick(View v) {
        cctSwiperController.onGetKsn();
    }

    public void onResetClick(View v) {
        cctSwiperController.stopCSwiper();
    }

    public void openCardReader(View v) {
        cctSwiperController.setAmount("1", "描述", "156", Template.FLAG_PAY_TRADE);
        cctSwiperController.onStartCSwiper(0, new byte[]{0x00, 0x00, 0x01}, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, 60);
    }

    public void openCardReaderQuery(View v) {
        cctSwiperController.setAmount("1", "描述", "156", Template.FLAG_PAY_QUERY);
        cctSwiperController.onStartCSwiper(0, new byte[]{0x00, 0x00, 0x01}, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, 60);
    }

    public void onScreenClick(View v) {
    }

    public void releaseDevice(View v) {
    }

    public void startGetCardNo(View v) {
    }

    public void clearLog(View v) {
        logsBuffer.delete(0, logsBuffer.length() - 1);
        txtvw.setText(logsBuffer.toString());
    }

    private StringBuffer logsBuffer = new StringBuffer();

    private class CSwiperStateChangedImp implements CSwiperStateChangedListener {
        @Override
        public void onAudioDetectStart() {

        }

        @Override
        public void onBluetoothBounding() {

        }

        @Override
        public void onAudioDetected() {

        }

        @Override
        public void onBluetoothBounded() {

        }

        @Override
        public void onConnectTimeout() {

        }

        @Override
        public void onGetKsnCompleted(String ksn) {
            logsBuffer.append(" == onGetKsnCompleted == ").append(ksn).append("\n");
            txtvw.setText(logsBuffer.toString());
        }

        @Override
        public void onWaitingForCardSwipe() {

        }

        @Override
        public void onDetectTrack() {

        }

        @Override
        public void onDetectIC() {

        }

        @Override
        public void onDecodingStart() {

        }

        @Override
        public void onDecodeError(int errorFlag) {

        }

        @Override
        public void onSwipeCardTimeout() {

        }

        @Override
        public void onDecodeCompleted(String formatID, String ksn, String encTracks, int track1Length, int track2Length, int track3Length, String randomNumber, String maskedPAN, String expiryDate, String cardHolderName, String cardType, String cardMAC, String iccData, boolean isIC, String pinblock, String handBrushWay) {
            logsBuffer.append(" == formatID == ").append(formatID).append(" == ksn == ").append(ksn).append(" == track1Length == ")
                    .append(track1Length).append(" == track2Length == ").append(track2Length).append(" == track3Length == ").append(track3Length)
                    .append(" == randomNumber == ").append(randomNumber).append(" == maskedPAN == ").append(maskedPAN).append(" == expiryDate == ")
                    .append(expiryDate).append(" == cardHolderName == ").append(cardHolderName).append(" == cardType == ").append(cardType).append(" == cardMAC == ").append(cardMAC)
                    .append(" == iccData == ").append(iccData).append(" == isIC == ").append(isIC).append(" == pinblock == ").append(pinblock).append(" == handBrushWay == ").append(handBrushWay).append("\n");
            txtvw.setText(logsBuffer.toString());

        }

        @Override
        public void onICResponse(int result, byte[] resultScript, byte[] data) {

        }

        @Override
        public void onError(int errorCode, String error) {

        }

        @Override
        public void onInterrupted() {

        }

        @Override
        public void onTradeCancel() {

        }

        @Override
        public void onDeviceFind(BluetoothDevice device) {
            bluetoothDialog.add(device);
        }
    }

}
